package com.example.icongyou.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.icongyou.R;
import com.example.icongyou.bean.AssignmentDisplay;

import java.util.ArrayList;
import java.util.List;

public class AssignmentAdapter extends RecyclerView.Adapter<AssignmentAdapter.ViewHolder> {
    private List<AssignmentDisplay> assignmentList;
    private OnItemClickListener listener;

    public AssignmentAdapter(List<AssignmentDisplay> assignmentList, OnItemClickListener listener) {
        this.assignmentList = assignmentList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_homework_teacher, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        AssignmentDisplay assignment = assignmentList.get(position);
        holder.bind(assignment, listener);
    }

    @Override
    public int getItemCount() {
        return assignmentList.size();
    }

    public void updateData(List<AssignmentDisplay> newList) {
        assignmentList = newList;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvGroupName;
        private TextView tvScore;

        ViewHolder(View itemView) {
            super(itemView);
            tvGroupName = itemView.findViewById(R.id.tv_homework_title);
            tvScore = itemView.findViewById(R.id.tv_score);
        }

        void bind(AssignmentDisplay assignment, OnItemClickListener listener) {
            // 显示格式：小组-人名
            tvGroupName.setText(assignment.getDisplayName());
            tvScore.setText(assignment.getStatusText());
            tvScore.setTextColor(assignment.getStatusColor());

            itemView.setOnClickListener(v -> listener.onItemClick(assignment));
        }
    }

    public interface OnItemClickListener {
        void onItemClick(AssignmentDisplay assignment);
    }
}